# 用户帐户相关功能：注册、短信、登录、注销
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from web.forms.account import RegisterModelForm, SendSmsForm
from django.conf import settings


def register(request):
    # 注册
    if request.method == 'GET':
        form = RegisterModelForm()
        return render(request, 'register.html', {'form': form})
    form = RegisterModelForm(data=request.POST)
    if form.is_valid():
        # 验证通过，写入数据库（密码要是密文）
        form.save()
        return JsonResponse({'status': True, 'data': '/login/'})
    return JsonResponse({'status': False, 'errors': form.errors})


def send_sms(request):
    # 发送短信
    # print(request.GET)
    # mobile_phone=request.GET.get('mobile_phone')
    # tpl=request.GET.get('tpl')
    # template_id=settings.TENCENT_SMS_TEMPLATE.get(tpl)

    # 实例化对象
    form = SendSmsForm(request, data=request.GET)
    # 只是校验手机号：不能为空，以及格式是否正确
    if form.is_valid():
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'errors': form.errors})
